<template>
  <el-dialog v-if="load" :close-on-click-modal="false" title="申请退货" v-model="visible" align-center width="1300px"
    :z-index="2000" @close="handleClose">
    <el-form ref="formRef" :model="form" label-width="110px">
      <div class="v-title mb-3">基础信息</div>
      <div class="grid grid-cols-4 gap-x-3">
        <el-form-item label="供应商">
          <el-input v-model="form.basic.supplier_name" placeholder="请输入" disabled />
        </el-form-item>
        <el-form-item label="供应商类型">
          <VSelect v-model="form.basic.purchase_type" placeholder="请选择" :options="options.purchase_type || []" filterable
            clearable disabled />
        </el-form-item>
        <el-form-item label="结算方式">
          <el-input v-model="form.basic.period_name" placeholder="请输入" disabled />
        </el-form-item>
        <el-form-item label="公司主体">
          <VSelect v-model="form.basic.company_id" placeholder="请选择" :options="options.company_id || []" filterable
            clearable disabled />
        </el-form-item>
        <el-form-item label="退货类型">
          <VSelect v-model="form.basic.return_type" placeholder="请选择"
            :options="(options.return_type || []).filter(d => [1, 2, 3].includes(d.value))" filterable clearable />
        </el-form-item>
        <el-form-item label="退货方式" prop="basic.return_method"
          :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <VSelect v-model="form.basic.return_method" placeholder="请选择" :options="options.return_method || []" filterable
            clearable />
        </el-form-item>
        <el-form-item label="收件人" prop="basic.contact_name"
          :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-input v-model="form.basic.contact_name" placeholder="请输入" class="w-full" />
        </el-form-item>
        <el-form-item label="联系电话" prop="basic.contact_tel"
          :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-input v-model="form.basic.contact_tel" placeholder="请输入" class="w-full" />
        </el-form-item>
        <el-form-item label="收货地址" required class="col-span-2">
          <Area v-model="form.basic" field="return" />
        </el-form-item>
        <el-form-item label="运费承担方" prop="basic.tracking_carrier"
          :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <VSelect v-model="form.basic.tracking_carrier" placeholder="请选择" :options="options.tracking_carrier || []"
            filterable clearable />
        </el-form-item>
        <el-form-item label="垫付运费" prop="basic.is_prepay">
          <VSelect v-model="form.basic.is_prepay" placeholder="请选择" :options="options.is_prepay || []" filterable
            clearable />
        </el-form-item>
        <el-form-item label="备注" class="col-span-full">
          <el-input v-model="form.basic.remark" placeholder="请输入" type="textarea"
            :autosize="{ minRows: 2, maxRows: 5 }" />
        </el-form-item>
      </div>
      <div class="v-title mb-3">采购单信息</div>
      <vxe-table ref="tableRef" :data="form.purchase" size="small">
        <vxe-column title="采购单号" :min-width="140">
          <template #default="{ row }">
            <VText :value="row.purchase_no" />
          </template>
        </vxe-column>
        <vxe-column title="sku" field="sku" :min-width="120">
          <template #default="{ row }">
            <VText :value="row.sku" />
          </template>
        </vxe-column>
        <vxe-column title="收货数量" field="deliver_num" :min-width="80" />
        <vxe-column title="质检数量" field="qa_num" :min-width="80" />
        <vxe-column title="良品数" field="quality_num" :min-width="80" />
        <vxe-column title="不良品数" field="reject_num" :min-width="80" />
        <vxe-column title="入库数" field="inbound_num" :min-width="80" />
        <vxe-column title="已退货数量" field="actual_return_num" :min-width="80" />
        <vxe-column title="可退数量" field="can_return_num" :min-width="80" />
        <vxe-column title="申请退货数量" :min-width="120">
          <template #default="{ row }">
            <el-input-number v-model="row.return_num" :controls="false" :min="0" :step="1" :precision="0"
              class="w-full" />
          </template>
        </vxe-column>
        <vxe-column title="操作" width="80">
          <template #default="{ rowIndex }">
            <el-button link type="danger" @click="form.purchase.splice(rowIndex, 1)">删除</el-button>
          </template>
        </vxe-column>
      </vxe-table>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings[2]" @click="create(2)">确 定</el-button>
      <el-button type="primary" :loading="loadings[1]" @click="create(1)">提 交</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import Area from '@/components/Area'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

// 表单处理
const formRef = ref()
const form = ref({})

// 打开弹窗
const open = async (p_ids) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = {
    basic: {},
    purchase: [],
  }
  loadings = reactive({})
  formRef?.value?.resetFields()

  try {
    const res = await api.selectDetail({ p_ids })
    res.data.purchase.map(d => {
      d.return_num = 0
      return d
    })
    form.value = res.data
  } catch (error) {
    return
  }
  form.value.basic.return_type = 1
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async (is_submit) => {
  const validator = await formRef?.value.validate()
  if (!validator) return
  loadings[is_submit] = true
  form.value.basic.is_submit = is_submit
  api.applyReturn(form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings[is_submit] = false
  })
}

defineExpose({ open })
</script>